using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace OpenComposite.EII.Controls.DataGridViewColumns
{
	#region Class: SelectWebServiceMethodColumn : DataGridViewColumn
	public class SelectWebServiceMethodColumn : DataGridViewColumn
	{
		public SelectWebServiceMethodColumn()
			: base(new SelectWebServiceMethodCell())
		{
		}
		public override DataGridViewCell CellTemplate
		{
			get
			{
				return base.CellTemplate;
			}
			set
			{
				if ( value != null &&
					 !value.GetType().IsAssignableFrom(typeof(SelectWebServiceMethodCell)) ) {
					throw new InvalidCastException("Must be a SelectWebServiceMethodCell.");
				}
				base.CellTemplate = value;
			}
		}
	}
	#endregion

	#region Class: SelectWebServiceMethodCell : DataGridViewCell, IDataGridViewEditingCell
	public class SelectWebServiceMethodCell : DataGridViewTextBoxCell, IDataGridViewEditingCell
	{
		#region Initialization
		public SelectWebServiceMethodCell()
			: base()
		{
		}
		#endregion

		#region Public Members

		#region Properties
		public override Type EditType
		{
			get
			{
				return typeof(SelectWebServiceMethodEditingControl);
			}
		}
		public override Type ValueType
		{
			get
			{
				return typeof(SelectedWebServiceMethod);
			}
		}
		public override object DefaultNewRowValue
		{
			get
			{
				return new SelectedWebServiceMethod();
			}
		}
		#endregion

		#region Methods
		public override void InitializeEditingControl(
			int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
		{
			base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
			SelectWebServiceMethodEditingControl ctrl =
				DataGridView.EditingControl as SelectWebServiceMethodEditingControl;
			ctrl.Value = (SelectedWebServiceMethod)this.Value;
		}
		#endregion

		#region IDataGridViewEditingCell Members

		public object EditingCellFormattedValue
		{
			get
			{
				return this.Value;
			}
			set
			{
				if ( !(value is SelectedWebServiceMethod) )
					throw new InvalidCastException("Must be a SelectedWebServiceMethod.");
				this.Value = value;
			}
		}

		public bool EditingCellValueChanged
		{
			get
			{
				return true;
			}
			set
			{
				
			}
		}

		public object GetEditingCellFormattedValue(DataGridViewDataErrorContexts context)
		{
			return this.Value;
		}

		public void PrepareEditingCellForEdit(bool selectAll)
		{
		}

		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected override void Paint(
			System.Drawing.Graphics graphics,
			System.Drawing.Rectangle clipBounds,
			System.Drawing.Rectangle cellBounds,
			int rowIndex,
			DataGridViewElementStates cellState,
			object value, object formattedValue,
			string errorText,
			DataGridViewCellStyle cellStyle,
			DataGridViewAdvancedBorderStyle advancedBorderStyle,
			DataGridViewPaintParts paintParts)
		{
			SelectWebServiceMethodEditingControl ctrl = new SelectWebServiceMethodEditingControl();
			ctrl.Value = value as SelectedWebServiceMethod;
			using ( Bitmap bmp = new Bitmap(cellBounds.Width, cellBounds.Height) ) {
				ctrl.DrawToBitmap(bmp, cellBounds);
				graphics.DrawImage(bmp, cellBounds);
			}
		}
		#endregion

		#endregion
	}
	#endregion

	#region Class: SelectWebServiceMethodEditingControl : UCSelectWebServiceMethod, IDataGridViewEditingControl
	[ToolboxItem(false)]
	public class SelectWebServiceMethodEditingControl : UCSelectWebServiceMethod, IDataGridViewEditingControl
	{
		#region Initialization
		public SelectWebServiceMethodEditingControl()
		{
			_dataGridView = null;
			_valueChanged = false;
			_rowIndex = -1;
		}
		#endregion

		#region Public Members

		#region IDataGridViewEditingControl Members

		public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
		{
			this.BackColor = dataGridViewCellStyle.BackColor;
			this.Font = dataGridViewCellStyle.Font;
			this.ForeColor = dataGridViewCellStyle.ForeColor;
			this.Padding = dataGridViewCellStyle.Padding;
		}

		public DataGridView EditingControlDataGridView
		{
			get
			{
				return _dataGridView;
			}
			set
			{
				_dataGridView = value;
			}
		}

		public object EditingControlFormattedValue
		{
			get
			{
				return this.Value;
			}
			set
			{
				this.Value = value as SelectedWebServiceMethod;
			}
		}

		public int EditingControlRowIndex
		{
			get
			{
				return _rowIndex;
			}
			set
			{
				_rowIndex = value;
			}
		}

		public bool EditingControlValueChanged
		{
			get
			{
				return _valueChanged;
			}
			set
			{
				_valueChanged = value;
			}
		}

		public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
		{
			return false;
		}

		public Cursor EditingPanelCursor
		{
			get { return base.Cursor; }
		}

		public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
		{
			return this.EditingControlFormattedValue;
		}

		public void PrepareEditingControlForEdit(bool selectAll)
		{
			// no preparation to be done
		}

		public bool RepositionEditingControlOnValueChange
		{
			get { return false; }
		}

		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected override void OnValueChanged()
		{
			_valueChanged = true;

			if ( this.EditingControlDataGridView != null )
				this.EditingControlDataGridView.NotifyCurrentCellDirty(true);

			base.OnValueChanged();
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private DataGridView _dataGridView;
		private bool _valueChanged;
		private int _rowIndex;
		#endregion

		#endregion
	}
	#endregion

	#region Class: SelectedWebServiceMethod
	public class SelectedWebServiceMethod
	{
		public SelectedWebServiceMethod()
		{
			_ws = null;
			_wm = null;
		}

		private Repository.WebService _ws;
		public Repository.WebService WebService
		{
			get { return _ws; }
			set { _ws = value; }
		}
		private Repository.WebMethod _wm;
		public Repository.WebMethod WebMethod
		{
			get { return _wm; }
			set { _wm = value; }
		}
	}
	#endregion
}
